//
//  YPWmcDrivingManager+Info.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/10.
//

import UIKit
import YPBedrock

extension YPWmcDrivingManager {
    /// 获取今天里，本地相册的媒体
    static func localAlbumTodayMediaList(mediaType: [WmcMedia.MediaType],
                                         success: YPBlock.OneParam<[WmcMedia]>? = nil,
                                         failure: YPBlock.OneParam<YPWmcError>? = nil)
    {
        let day =  WmcDateManager.shared.networkDate.value ?? .init()
        localAlbumMediaList(mediaType: mediaType, day: day, success: success, failure: failure)
    }

    /// 获取某一天里，本地相册的媒体
    static func localAlbumMediaList(mediaType: [WmcMedia.MediaType],
                                    day: Date,
                                    success: YPBlock.OneParam<[WmcMedia]>? = nil,
                                    failure: YPBlock.OneParam<YPWmcError>? = nil)
    {
        let startDate: Date = .YP.Create(year: day.yp.year,
                                         month: day.yp.month,
                                         day: day.yp.day,
                                         hour: 0,
                                         minute: 0,
                                         second: 0) ?? .init()
        let endDate: Date = .YP.Create(year: day.yp.year,
                                       month: day.yp.month,
                                       day: day.yp.day,
                                       hour: 23,
                                       minute: 59,
                                       second: 59) ?? .init()
        localAlbumMediaList(mediaType: mediaType, startDate: startDate, endDate: endDate, success: success, failure: failure)
    }

    /// 获取某段时间里，本地相册的媒体
    static func localAlbumMediaList(mediaType: [WmcMedia.MediaType],
                                    startDate: Date,
                                    endDate: Date,
                                    success: YPBlock.OneParam<[WmcMedia]>? = nil,
                                    failure: YPBlock.OneParam<YPWmcError>? = nil)
    {
        var returnList: [WmcMedia] = []
        WmcLocalMediaAlbumDBManager.searchMediaData() { list in
            for dbModel in list {
                if dbModel.saveTime > startDate,
                   dbModel.saveTime < endDate
                {
                    if mediaType.contains(dbModel.mediaType),
                       let filePath = dbModel.mediaInfoType.filePath { // 需要的类型
                        switch dbModel.mediaType {
                        case .image:
                            let wmcImage = WmcImage(filePath: filePath)
                            returnList.append(wmcImage)
                        case .video:
                            let wmcVideo = WmcVideo(filePath: filePath)
                            returnList.append(wmcVideo)
                        default: break
                        }
                    }
                }
            }
            success?(returnList)
        }
    }

    /// 获取所有本地相册的媒体
    static func localAlbumAllMediaList(mediaType: [WmcMedia.MediaType],
                                       page: Int,
                                       pageSize: Int,
                                       success: YPBlock.OneParam<[WmcMedia]>? = nil,
                                       failure: YPBlock.OneParam<YPWmcError>? = nil)
    {
        WmcLocalMediaAlbumDBManager.searchMediaData(page: page, pageSize: pageSize) { list in
            var returnList: [WmcMedia] = []
            for dbModel in list {
                if mediaType.contains(dbModel.mediaType),
                   let filePath = dbModel.mediaInfoType.filePath { // 需要的类型
                    switch dbModel.mediaType {
                    case .image:
                        let wmcImage = WmcImage(filePath: filePath)
                        returnList.append(wmcImage)
                    case .video:
                        let wmcVideo = WmcVideo(filePath: filePath)
                        returnList.append(wmcVideo)
                    default: break
                    }
                }
            }
            success?(returnList)
        }
    }
}
